/**
* @Title: warning_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @date 2014年10月27日 下午4:43:38
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('WarningAssessListCtrl', ['$scope','FwaTabsetService','WarningAddService','$modal','BaseURL','Modal','WarningService',
	                                function($scope,FwaTabsetService,WarningAddService,$modal,baseUrl,Modal,WarningService){
		$scope.warningService = WarningService;
		WarningService.initMedicalOrgInfoList();
		$scope.form = {};
		$scope.form.warnDate = {};
		var currentPage = 1;
		var cellTemplate = 
			'<td>'+
				'<a  ng-click="clickLink(\'edit\',data)"">{{data.tranName}}</a>'+
			'</td>',
			cellTemplateView = 
				'<td>'+
				   '<a title=\'{{data.warnNoBiz}}\' ng-click="clickLink(\'warnQueryAssess.warningView\',data)"">{{data.warnNoBiz}}</a>'+
			    '</td>',
			indexNo = '<td>'+
				'{{$index+1}}'+
			'</td>';
       $scope.columns = [
        {label:'预警编号',width:'10%',cellClass:'text-left', cellTemplate:cellTemplateView},
        {label:'预警事件名称',map:'summary',width:'10%'},
        {label:'机构名称',map:'medicalOrgName',width:'10%'},
        {label:'处理方式',map:'process',width:'8%'},
        {label:'预警时段',map:'happenDate',width:'14%'},
        {label:'反馈截止时间',map:'feedbackEndDate',width:'10%'},
        {label:'受理时间',map:'warnDate',width:'10%'},
        {label:'审核时间',map:'auditDate',width:'10%'},
        {label:'事件状态',map:'actName',width:'10%'},
        {label:'操作',width:'8%',cellClass:'text-left', cellTemplate:cellTemplate}

    ];
       $scope.clickLink = function (url,data){
    	   if(url==='edit'&&data.actNo==='N2014007'){
    		   FwaTabsetService.selectTab('warnQueryAssess.warningView',{warnNo:data.warnNo,actNo:'resultAssess',isBack:'Y'},false);
    	   }else if(url==='edit'){
    		   FwaTabsetService.selectTab('warnQueryAssess.warningAssessWarn',{warnNo:data.warnNo,urlFlag:''},false);
    	   }else{
    		   FwaTabsetService.selectTab(url,{warnNo:data.warnNo,actNo:'resultAssess',isBack:'N'},false);
    	   }
       }

		//每页显示数
        var start = 0,
            limit = 20;
        
//        $scope.form = {
//            page:"0",
//            pageSize:"20"
//        };

        
        var params = function (){
			var warnNoBiz=null;
			if($scope.form.warnNoBiz&&!angular.isUndefined($scope.form.warnNoBiz)){
				warnNoBiz = $scope.form.warnNoBiz;
			}
			var medicalOrgCode=null;
			if($scope.form.medicalOrgCode&&!angular.isUndefined($scope.form.medicalOrgCode)){
				medicalOrgCode = $scope.form.medicalOrgCode;
			}
			var summary=null;
			if($scope.form.summary&&!angular.isUndefined($scope.form.summary)){
				summary = $scope.form.summary;
			}
			var feedbackEndDate=null;
			if($scope.form.feedbackEndDate&&!angular.isUndefined($scope.form.feedbackEndDate)){
				feedbackEndDate = $scope.form.feedbackEndDate;
			}
//			var actNo=null;
//			if($scope.form.actNo&&!angular.isUndefined($scope.form.actNo)){
//				actNo = $scope.form.actNo;
//			}
			var startDate=null;
			if($scope.form.warnDate.start&&!angular.isUndefined($scope.form.warnDate.start)){
				startDate = $scope.form.warnDate.start;
			}
			var endDate=null;
			if($scope.form.warnDate.end&&!angular.isUndefined($scope.form.warnDate.end)){
				endDate = $scope.form.warnDate.end;
			}
			
			if(parseInt(currentPage) > 1){
                  start = (parseInt(currentPage) - 1) * limit;
            }
			var params = {
					warnNoBiz:warnNoBiz,
					medicalOrgCode:medicalOrgCode,
					summary:summary,
					feedbackEndDate:feedbackEndDate,
//					actNo:actNo,
					startDate:startDate,
					endDate:endDate,
					start:start,
					limit:limit
			}
			return params;
		}
        

          //查询
          function query(){
          	WarningService.warningAssessQuery(params()).success(function(res){
                  $scope.tableData = res.result;
                  $scope.totalCount = res.total;
              });
          }

  		//分页查询
          $scope.$on('pageTool.skipPage',function(e,v){
              start = limit*(v-1);
              $scope.form.page = start;
              //$scope.onSubmit();
              query();
          });

          //页面点击查询
          $scope.onSubmit = function(){
          	start=0;
          	$scope.currentPage=1;
          	 query();
          };
		
        //加载数据
        $scope.onSubmit();
        
         //重置
        $scope.resetBut = function(){
			$scope.form = {};
			$scope.form.warnDate = {};
		}
        //--------------------------批量确认
        //责任人意见
        $scope.auditResultData=[];
        function selectAuditResultDate(){
			var param=['WARN_CHECK_AUDIT_RESULT']
			WarningAddService.getHcpmsCodeList(param).success(function(response){
				$scope.auditResultData=response.result;
			});			
		}
        selectAuditResultDate();
        
/*        $scope.batchAssessList=null;
        function getBatchAssessData(){
        	var batchAssessList=[];//选中的批量
        	var getSubmitList=[];//选中且状态不为撤销的批量
        	angular.forEach($scope.tableData,function(data){
        		if(data.checked){
        			batchAssessList.push(data);
        		} 
        	});
        	if(batchAssessList.length>0){
        		for(var i=0;i<batchAssessList.length;i++){
        			if(batchAssessList[i].actNo==='N2014007'){//撤销
        				return false;
        			}else{
        				getSubmitList.push(batchAssessList[i]);
        			}
        		}
        		
        		$scope.batchAssessList=getSubmitList;
        	}
        }
        */
        //批量确认
        $scope.submitBatchWarning=function(){
        	 $scope.batchAssessList=null;           
             	var batchAssessList=[];//选中的批量
             	var getSubmitList=[];//选中且状态不为撤销的批量
             	var checkbatchData=true;
             	angular.forEach($scope.tableData,function(data){
             		if(data.checked){
             			batchAssessList.push(data);
             		} 
             	});
             	if(batchAssessList.length>0){
             		for(var i=0;i<batchAssessList.length;i++){
             			if(batchAssessList[i].actNo==='N2014007'){//撤销
             				checkbatchData= false;
             			}else{
             				getSubmitList.push(batchAssessList[i]);
             			}
             		}
             		
             		$scope.batchAssessList=getSubmitList;
             	}            
        	if(checkbatchData===false){
        		Modal.alert('不能批量提交含有撤销的数据');
         		return;
        	}
        	if(!$scope.batchAssessList){
        		 Modal.alert('请选择批量确认的数据！');
        		return;
        	}
        	var scope=$scope.$new();
			scope.auditResultData= $scope.auditResultData;
			scope.batchAssessList=$scope.batchAssessList;
			 var modalInstance = $modal.open({
				  templateUrl: baseUrl + 'views/hcpms/warning/warnQuery/warningAssessBatchDialog_view.html',			     
			      backdrop:'static',
				  size:'sm',
				  scope:scope,
				  controller: function($scope, $modalInstance,Modal,CommunityService){
					  $scope.batchForm = {};					 
					  //关闭弹窗
					  $scope.onOk = function() {
						  $modalInstance.dismiss();
					  }					  
					  //确定事件
					  $scope.confirm = function() {						 
						    if(!$scope.batchForm.opinion){
								Modal.alert("责任人意见不能为空!");
								return false;
							}
							if(($scope.batchForm.opinion === '02'||$scope.batchForm.opinion=== '03')&& !$scope.batchForm.opinionDesc){
								Modal.alert("不同意时,意见说明不能为空!");
								return false;
							}
							var date=$scope.batchForm.date;
							if(!date||!date.start||!date.end){
								   Modal.alert("后续跟踪时段不能为空!");
								   return false;
							  }
							$scope.batchForm.startDate=date.start;
							$scope.batchForm.endDate=date.end;
							var param = {								
									assessResult:$scope.batchForm,
									batchWarnList:$scope.batchAssessList
							}	 
					        $scope.batchForm.loading=true;
							WarningAddService.assessWarnBatchProcess(param).success(function(res){
								Modal.alert(res.msg);
							  $scope.batchForm.loading=false;
							  if(res.state=="0"){
								  $modalInstance.dismiss();
								  $scope.onSubmit();
							  }
				            });
						//$modalInstance.close($scope.form);
					  }
				  }
			 });
        }
        //-------------批量结束
	}]);
})();